package com.banyiyi.simple.pattern.file;

import java.io.*;

/**
 * @ClassName AccessRendomFileDemo
 * @Description TODO
 * @Author yangwm
 * @Date 2023/2/23 9:01
 * @Version 1.0
 */
public class RandomAccessFileDemo {

    // 文件读写
    static String path = "C:\\Users\\MECHREVO\\Desktop\\新建文本文档.txt";
    static String outPath = "C:\\Users\\MECHREVO\\Desktop\\新建文本文档-副本.txt";
    // 定义临时存放分片的文件夹
    static String dir = "C:\\Users\\MECHREVO\\Desktop\\temp";
    // 定义切割长度
    static int division_len = 64;


    public static void main(String[] args) {

        /*try {
            // 读取
            RandomAccessFile raf = new RandomAccessFile(path,"rw");
            // 写出
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath));

            // 获取当前指针位置
            long pointer = raf.getFilePointer();
            System.out.println("读取文件开始之前，指针所在位置："+pointer);
            // 文件读取
            byte[] b = new byte[1024];
            while (raf.read(b) != -1) {
                System.out.println(new String(b));
                out.write(b);
            };
            System.out.println("文件读取之后，指针所在位置："+raf.getFilePointer());

            out.flush();
            out.close();
            raf.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        // try-with-release 写法
        /*try(RandomAccessFile raf = new RandomAccessFile(path,"rw");
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath))){
            // 获取当前指针位置
            long pointer = raf.getFilePointer();
            System.out.println("读取文件开始之前，指针所在位置："+pointer);
            // 设置文件读取从头开始
            raf.seek(128);
            // 文件读取
            byte[] b = new byte[16];
            int len = 0;
            while ((len = raf.read(b)) != -1) {
                System.out.println("循环体中指针位置"+raf.getFilePointer());
                System.out.println("循环体中len长度"+ len);
                if(new Long(128).equals(raf.getFilePointer())) {
                    System.out.println("休息两秒钟，当前指针位置是："+raf.getFilePointer());
                    Thread.sleep(2000);
                    System.out.println("从当前位置进行换行");
                    // 获取当前系统换行符
                    String s = System.lineSeparator();
                    out.write(s.getBytes());
                    break;
                }
                System.out.println(new String(b));
                out.write(b, 0,len);
            };
            System.out.println("文件读取之后，指针所在位置："+raf.getFilePointer());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        // 分片
        /*File file = new File(path);
        long length = file.length();
        long l = length / division_len;
        if(length % division_len > 0) l++;
        for (long i = 0; i < l; i++) {
           String outPath = dir+"\\dir_"+i+".txt";
           divisionFile(path, outPath, i * division_len);
        }*/

        // 合并
        File file1 = new File(dir);
        int length1 = file1.listFiles().length;
        for (int i = 0; i < length1; i++) {
            String inPath = dir+"\\dir_"+i+".txt";
            mergeFile(inPath,outPath,i*division_len);
        }


    }

    /**
     * 文件分割
     * @param input 读取文件路径
     * @param output 输出文件路径
     * @param start 起始位置
     */
    private static void divisionFile(String input, String output,long start){
        // 文件夹不存在，则创建
        File file = new File(dir);
        if(!file.exists()){
            file.mkdir();
        }
        // 定义文件输入流和输出流
        // try-with-release
        try(BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output));
            RandomAccessFile raf = new RandomAccessFile(input,"rw")){

            // 定义分片大小， 分割和合并要保持一致 64
            // 设置读取指针位置
            raf.seek(start);
            // 读取 分割
            byte[] b = new byte[division_len];
            int len = 0;
            while((len = raf.read(b)) != -1){
                out.write(b);
                break;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 合并文件并输出
     * @param input
     * @param output
     * @param start
     */
    private static void mergeFile(String input,String output,long start){
        // try-with-release
        try(BufferedInputStream in = new BufferedInputStream(new FileInputStream(input));
            RandomAccessFile raf = new RandomAccessFile(output,"rw")){

            // 设置文件拼接位置
            raf.seek(start);

            byte[] b = new byte[division_len];
            while (in.read(b) != -1){
                raf.write(b);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
